package com.apes.hr.base.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.hr.base.model.SpecialInfo;
import com.apes.hr.base.repository.SpecialInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 人员特殊信息记录
 *
 * @author wuq
 * @create 2018-08-22 18:51
 */
@Service("hrSpecialInfoService")
public class SpecialInfoService extends DomainService {

    @Autowired
    public SpecialInfoRepository specialInfoRepository;

    public SpecialInfo save(SimpleRequest request) {
        SpecialInfo specialInfo = request.getO(SpecialInfo.class);
        return specialInfoRepository.saveAndFlush(specialInfo);
    }

    /**
     * 检查数据判断，判读当前的输入版本信息
     *
     * @param request
     */
    public void checkData(SimpleRequest request) {
        String id = request.get("id");
        int version = request.get("version");

        // 从数据库中取数，检查数据
        SpecialInfo oldSpecialInfo = specialInfoRepository.findOne(id);
        if (oldSpecialInfo == null) throw new RuntimeException("人员信息记录【" + id + "】在系统中不存在。");
        if (oldSpecialInfo.getVersion() != version) throw new RuntimeException("记录已更新或已删除，请重新提交！");
    }

    /**
     * 功能：修改人员信息记录
     *
     * @param request
     * @return
     */
    public SpecialInfo update(SimpleRequest request) {
        // 检查数据
        checkData(request);

        SpecialInfo specialInfo = request.getO(SpecialInfo.class);
        return specialInfoRepository.saveAndFlush(specialInfo);
    }

    /**
     * 功能：删除人员信息记录
     * @param request
     * @return
     */
    public SpecialInfo delete(SimpleRequest request) {
        // 检查数据
        checkData(request);

        SpecialInfo specialInfo = request.getO(SpecialInfo.class);
        specialInfo.setValid(false);
        return specialInfoRepository.saveAndFlush(specialInfo);
    }


    public SpecialInfo findOne(String id) {
        return specialInfoRepository.findOne(id);
    }


}